package com.itheima.pay.controller;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.common.models.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("easypay")
public class EasyPayController {


    @Autowired
    Config config;

    /**
     * 预下单：返回二维码的链接，二维码链接生成二维码图片
     * https://openapi.alipay.com/gateway.do   生产环境地址
     * https://openapi.alipaydev.com/gateway.do  沙箱环境地址
     */
    @GetMapping("precreate/{orderNo}")
    public AlipayTradePrecreateResponse precreate(@PathVariable String orderNo) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradePrecreateResponse response = Factory.Payment
                    .FaceToFace().preCreate("华为 P60",
                            orderNo, "1");


            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response);
                System.out.println("调用成功");
                return response;
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
        return null;
    }


    /**
     * 查询支付结果
     */
    @GetMapping("query/{orderNo}")
    public AlipayTradeQueryResponse queryOrder(@PathVariable String orderNo) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeQueryResponse response = Factory.Payment.Common().query(orderNo);

            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
                return response;
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
        return null;


    }

    /**
     * 退款
     */
    @GetMapping("refund/{orderNo}/{money}/{refundNo}")
    public AlipayTradeRefundResponse refund(@PathVariable String orderNo ,@PathVariable String money,@PathVariable String refundNo)  throws  Exception{
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeRefundResponse response = Factory.Payment.Common()
                    .optional("out_request_no", refundNo)
                    .refund(orderNo, money);

            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
                return response;
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
        return null;
    }



    /**
     * 退款查询
     * @return
     */
    @GetMapping("refund/{orderNo}/{refundNo}")
    public AlipayTradeFastpayRefundQueryResponse queryrefund(@PathVariable String orderNo, @PathVariable String refundNo)  throws  Exception{
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(config);
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
            AlipayTradeFastpayRefundQueryResponse response = Factory.Payment.Common()
                    .queryRefund(orderNo,refundNo);

            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println("调用成功");
                return response;
            } else {
                System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
        return null;
    }

}
